package com.uenpay.im.ui.old.activity.red;

import com.qiyunxin.android.http.net.HttpRequestType;
import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.invokeitems.HttpRequestHandle;

/**
 * @author SL
 * @created 2016-1-14 17:29:41
 * @copyRight http://www.qiyunxin.com
 * @function
 */
public class RedHandler {

	public interface ICreateRedListener {
		void onCreateRedResult(String packetsid, int status, String msg);
	}

	/**
	 * 创建红包
	 * 
	 * @param amount
	 * @param number
	 * @param type
	 * @param blessing
	 * @param paypassword
	 * @param mCreateRedListener
	 */
	public void createRed(String amount, String number, int type, String blessing, String paypassword,
			final ICreateRedListener mCreateRedListener) {
		if (!Utils.isNetworkConnected(IMApplication.getAppContext())) {
			IMApplication.showToast(R.string.network_disable);
			return;
		}
		CreateRedInvokItem item = new CreateRedInvokItem(amount, number, type, blessing, paypassword);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					CreateRedInvokItem.CreateRedInvokItemResult result = ((CreateRedInvokItem) resultObject).getOutput();
					if (result != null) {
						mCreateRedListener.onCreateRedResult(result.packetsid, result.status, result.msg);
					} else {
						mCreateRedListener.onCreateRedResult("", -1, failReason);
					}
				} else {
					mCreateRedListener.onCreateRedResult("", -1, failReason);
				}
			}
		});
	}

	public interface ISendRedResult {
		void onSendResult(int status, String msg);
	}

	/**
	 * 
	 * @param packetsid
	 *            红包id
	 * @param tagetid
	 *            私包时为用户custid ， 群包时为群chatid
	 * @param tagettype
	 *            1为私包 2为群包
	 */
	public void sendRed(String packetsid, String tagetid, int tagettype, final ISendRedResult mSendRedResult) {
		SendRedInvokItem item = new SendRedInvokItem(packetsid, tagetid, tagettype);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					SendRedInvokItem.SendRedInvokItemResult result = ((SendRedInvokItem) resultObject).getOutput();
					if (result != null) {
						mSendRedResult.onSendResult(result.status, result.msg);
					}
				} else {
					mSendRedResult.onSendResult(-1, failReason);
				}

			}
		});
	}

	public interface IPickRedListener {
		void onPickRed(int status, String msg, ReceiveRedEntity mReceiveRedEntity);
	};

	/**
	 * 捡红包
	 * 
	 * @param packetsid
	 * @param mPickRedListener
	 */
	public static void pickRed(String packetsid, final IPickRedListener mPickRedListener) {
		PickRedInvokItem item = new PickRedInvokItem(packetsid);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {

					PickRedInvokItem.PickRedInvokItemResult result = ((PickRedInvokItem) resultObject).getOutput();
					if (result != null) {
						mPickRedListener.onPickRed(result.status, result.msg, result.mReceiveRedEntity);
					}
				} else {
					mPickRedListener.onPickRed(-1, failReason, null);
				}
			}
		});
	}

	public interface IOpenRedListener {
		void onOpenRed(int status, String msg, ReceiveRedEntity mReceiveRedEntity);
	};

	/**
	 * 拆红包
	 * 
	 * @param packetsid
	 * @param mOpenRedListener
	 */
	public void openRed(String packetsid, final IOpenRedListener mOpenRedListener) {
		OpenRedInvokItem item = new OpenRedInvokItem(packetsid);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {

					OpenRedInvokItem.OpenRedInvokItemResult result = ((OpenRedInvokItem) resultObject).getOutput();
					if (result != null) {
						mOpenRedListener.onOpenRed(result.status, result.msg, result.mReceiveRedEntity);
					}

				} else {
					mOpenRedListener.onOpenRed(-1, failReason, null);
				}
			}
		});
	}

	public interface ITanksRedListener {
		void onResult(int status, String msg);
	}

	/**
	 * 答谢红包
	 * 
	 * @param packetsid
	 * @param thankswords
	 * @param tanksRedListener
	 */
	public static void returnTanksRed(String packetsid, String thankswords, final ITanksRedListener tanksRedListener) {
		ReturnThanksRedInvokItem item = new ReturnThanksRedInvokItem(packetsid, thankswords);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {

					ReturnThanksRedInvokItem.ReturnThanksRedInvokItemResult result = ((ReturnThanksRedInvokItem) resultObject).getOutput();
					if (result != null) {
						tanksRedListener.onResult(result.status, result.msg);
					}

				} else {
					tanksRedListener.onResult(-1, failReason);
				}
			}
		});
	}

}
